<template>
  <div class="com-container">
    <div class="title" :style="comStyle">
      <span>{{'▎   ' +' '+showTitle}}</span>
      <span class="iconfont title-icon" @click="showChoice = !showChoice"
        :style="comStyle">&#xe6eb;</span
      >
      <div class="select-con" v-show="showChoice" :style="marginStyle">
        <div class="select-item" v-for="item in selectTypes" :key="item.key" @click="handleSelect(item.key)">{{item.text}}</div>
      </div>
    </div>
    <div class="com-chart" ref="userlogin"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getThemeValue } from '@/utils/theme_utils'
export default {
  name: "UserLogin",
  data() {
    return {
      mychart: null,
      allData: null,
      showChoice: false,
      choiceType: "province",
      titleFontSize: 0,
    };
  },
  created() {
    //在组件创建完成之后进行回调函数的注册
    this.$socket.registerCallBack('loginData',this.getData)
  },
  mounted() {
    this.initChart();
    //this.getData();
    //发送数据给服务器，对服务器请求数据
    this.$socket.send({
      action: 'getData',
      socketType: 'loginData',
      chartName: 'userlogin',
      value: ''
    })
    window.addEventListener("resize", this.screenAdapter);
    //在页面加载完成的时候，主动进行屏幕的适配
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    //在组件销毁之后进行回调函数的取消
    this.$socket.unRegisterCallBack('loginData')
  },
  computed: {
    selectTypes(){
      if(!this.allData){
        return []
      }else{
        return this.allData.type.filter(item => {
          return item.key != this.choiceType
        })
      }
    },
    showTitle(){
      if(!this.allData){
        return ''
      }else{
        return this.allData[this.choiceType].title
      }
    },
    comStyle(){
      return {
        fontSize: this.titleFontSize + 'px',
        color: getThemeValue(this.theme).titleColor
      }
    },
    marginStyle(){
      return {
        marginLeft: this.titleFontSize + 'px'
      }
    },
    ...mapState(['theme'])
  },
  methods: {
    initChart() {
      this.mychart = this.$echarts.init(this.$refs.userlogin, this.theme);
      const initOption = {
        grid: {
          top: "20%",
          left: "3%",
          right: "6%",
          bottom: "3%",
          containLabel: true, //距离是包含坐标轴上的文字
        },
        xAxis: {
          type: "category",
        },
        yAxis: {
          type: "value",
        },
        tooltip: {
          show: true,
        },
        series: [
          {
            type: "bar",
            label: {
              show: true,
              position: "top",
              textStyle: {
                color: "white",
              },
            },
            itemStyle: {
              color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "#d4a4eb",
                },
                {
                  offset: 1,
                  color: "#f7c5a0",
                },
              ]),
            },
          },
        ],
      };
      this.mychart.setOption(initOption);
    },
    getData(ret) {
      //const { data: ret } = await this.$http.get("userlogin");
      console.log(ret, "userlogin");
      this.allData = ret;
      this.updateChart();
    },
    updateChart() {
      const datalist = this.allData[this.choiceType].data;
      const xdata = datalist.map((item) => {
        return item.name;
      });
      const ydata = datalist.map((item) => {
        return item.value;
      });
      const dataOption = {
        xAxis: { data: xdata },
        series: [
          {
            data: ydata,
          },
        ],
      };
      this.mychart.setOption(dataOption);
    },
    //当浏览器的大小发生变化时，调用该方法，完成屏幕的适配
    screenAdapter() {
      this.titleFontSize = (this.$refs.userlogin.offsetWidth / 100) * 3.6;
      const adapterOption = {
        series: [
          {
            barWidth: this.titleFontSize,
            itemStyle: {
              barBorderRadius: [
                this.titleFontSize / 2,
                this.titleFontSize / 2,
                this.titleFontSize / 2,
                this.titleFontSize / 2,
              ],
            },
          },
        ],
      };
      this.mychart.setOption(adapterOption);
      //手动调用图表对象的resize
      this.mychart.resize();
    },
    handleSelect(currentType){
        this.choiceType = currentType
        this.updateChart()
        this.showChoice = false
    }
  },
  watch: {
    theme() {
      this.mychart.dispose()//销毁当前图表
      this.initChart()//重新以最新主题初始化图表对象
      this.screenAdapter()
      this.updateChart()
    }
  },
};
</script>

<style>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;
  color: white;
  font-weight: bold;
}
.title-icon {
  margin-left: 10px;
  cursor: pointer;
}
.select-con {
  background-color: rgba(41,52,65,1);
}
</style>